//
//  LoginController.m
//  YL8G
//
//  Created by gzty1 on 12-8-5.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginController.h"
#import "TextFieldDialog.h"
#import "Instance.h"
#import "DialogUtil.h"
#import "Request.h"
#import "ASIHTTPRequest.h"
#import "BaseResponseData.h"

@implementation LoginController
@synthesize delegate=delegate_;

-(void)dealloc
{
    [dialog_ release];
    delegate_=nil;
    [super dealloc];
}

-(void)login
{
    if([INSTANCE.contextData.cookie length]==0)
    {
        [INSTANCE.contextData importLoginInfo];
        BOOL autoLogin=[INSTANCE.contextData.userName length]>0 && [INSTANCE.contextData.password length]>0;	
        if (autoLogin) 
        {
            NSString* username=INSTANCE.contextData.userName;
            NSString* password=INSTANCE.contextData.password;
            
            [self loginWithUsername:username password:password];
        }
        else
        {
            [self showLoginDialog];
        }
    }
}

-(void)showLoginDialog
{
    [dialog_ release];
    dialog_=[[TextFieldDialog alloc] initWithTitle:@"请使用天涯社区账号登陆" 
                                  placeholderArray:[NSArray arrayWithObjects:@"用户名",@"密码", nil] 
                                       buttonTitle:@"登录" 
                                      buttonTitle2:nil 
                                        closeImage:[UIImage imageNamed:@"delete.png"]
                                          delegate:self];
    [dialog_ setSecureTextEntryForTextFieldAtIndex:1];
    [dialog_ show];
}

+(void)logout
{
    INSTANCE.contextData.cookie=nil;
     [INSTANCE.contextData deletePersistentLoginInfo];
}

+(BOOL)hasLogin
{
    return [INSTANCE.contextData.cookie length]>0;
}

-(void)loginWithUsername:(NSString*)username password:(NSString*)password
{
    if ([username length] == 0)
	{
		[DialogUtil postAlertWithMessage:@"用户名不能为空"];
		return;
	}
	
	if ([password length] == 0)
	{
		[DialogUtil postAlertWithMessage:@"密码不能为空"]; 
		return;
	}
    
	ASIHTTPRequest* request=[Request loginWithUserName:username passWord:password delegate:self];
    request.tag=200;
	[DialogUtil showModalWaitingViewWithMessage:@""];
}

#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString* username=[dialog_ textFieldAtIndex:0].text;
    NSString* password=[dialog_ textFieldAtIndex:1].text;
    
	[self loginWithUsername:username password:password];
}

#pragma mark -
#pragma mark ASIHTTPRequest
- (void)requestFinished:(ASIHTTPRequest *)request 
{
    [DialogUtil hideWaitingView];
    NSLog(@"requestFinished \n %@",[request responseString]);
    
    BaseResponseData* data=[BaseResponseData responseDataWithRequest:request];
    BOOL success=[data success];
    if(success)
    {
        [INSTANCE.contextData parseDataDic:data.dataDic];
        
        //存盘登录信息
        if(dialog_)
        {
            INSTANCE.contextData.userName=[dialog_ textFieldAtIndex:0].text;
            INSTANCE.contextData.password=[dialog_ textFieldAtIndex:1].text;
            [INSTANCE.contextData exportLoginInfo];
        }
        
        [delegate_ loginControllerSuccess:self];
    }
    else
    {
        [INSTANCE.contextData deletePersistentLoginInfo];
        [self showLoginDialog];
        [DialogUtil postAlertWithMessage:[data errorMessage]];
    }
    
    [dialog_ release];
    dialog_=nil;
}

@end
